<template>
  <!-- 监测点新增具体form表单 -->

  <el-form label-suffix="：" label-position="right" ref="form" :rules="rules" :model="currentRow" label-width="115px"
           class="work-point-form">
    <el-collapse v-model="activeName" accordion>
      <el-collapse-item title="基础信息" name="基础信息">
        <el-row>
          <el-col :span="8">
            <el-form-item label="监测点名称" prop="name">
              <el-input v-model="currentRow.name" placeholder="输入监测点名称"  clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="监测类型" prop="monitorType">
              <el-select v-model.trim="currentRow.monitorType" filterable placeholder="选择监测类型" @change="changeMonitorType()">
                <el-option v-for="item in EnumProjectMonitorTypeList" :key="item.code" :label="item.name"
                           :value="item.code"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="起止时间">
              <el-date-picker
                value-format="timestamp"
                v-model="timerArray"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
<!--          <el-col :span="6">-->
<!--            <el-form-item label="是否推送UDP" v-if="currentRow.monitorType !== 3">-->
<!--              <el-radio-group v-model="currentRow.isPushUdp">-->
<!--                <el-tooltip class="item" effect="dark" content="推送" placement="top-start">-->
<!--                  <el-radio-button :label=1>是</el-radio-button>-->
<!--                </el-tooltip>-->
<!--                <el-tooltip class="item" effect="dark" content="不推送" placement="top-start">-->
<!--                  <el-radio-button :label=0>否</el-radio-button>-->
<!--                </el-tooltip>-->
<!--              </el-radio-group>-->
<!--            </el-form-item>-->
<!--          </el-col>-->

          <el-col :span="16">
            <el-form-item label="数据采集方式">
              <el-radio-group v-model="currentRow.isAutoRecord">
                <el-tooltip class="item" effect="dark" content="人工导入" placement="top-start">
                  <el-radio-button :label=0>人工监测</el-radio-button>
                </el-tooltip>
                <el-tooltip class="item" effect="dark" content="设备上传" placement="top-start">
                  <el-radio-button :label=1>自动监测</el-radio-button>
                </el-tooltip>
                <el-tooltip class="item" effect="dark" content="接口推送" placement="top-start">
                  <el-radio-button :label=2>接入监测</el-radio-button>
                </el-tooltip>
<!--                选择深部的时候才会展示-->
                <el-tooltip v-if=" currentRow.monitorType === 1 " class="item" effect="dark" content="APP上传">
                  <el-radio-button :label=3>滑动侧斜监测</el-radio-button>
                </el-tooltip>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="推送sn" prop="pushSn"  v-if="hasPermission('project:push:rule')">
              <el-input v-model="currentRow.pushSn" placeholder="输入推送sn " :maxlength="50" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <template >
          <el-row>
            <el-col :span="8">
              <el-form-item label="经度" prop="longitude">
                <el-input v-model="currentRow.longitude" placeholder="输入经度" type="number" :maxlength="50"
                          clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="纬度" prop=" latitude">
                <el-input v-model="currentRow.latitude" placeholder="输入纬度" type="number" :maxlength="50"
                          clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="高程" prop="height">
                <el-input v-model="currentRow.height" placeholder="输入高程" type="number" :maxlength="50"
                          clearable></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </template>
        <template v-if="currentRow.monitorType === 8">
          <el-form-item label="高度">
            <el-input v-model="currentRow.holeDepth" placeholder="输入高度" type="number" :maxlength="10" clearable>
              <i slot="suffix">m</i>
            </el-input>
          </el-form-item>
          <el-form-item label="x轴朝向">
            <el-select v-model.trim="currentRow.direction" clearable placeholder="选择x轴朝向">
              <el-option v-for="item in DirectionList" :key="item.code" :label="item.name"
                         :value="item.code"></el-option>
            </el-select>
          </el-form-item>
        </template>
        <!--       当监测类型为深部位移，地下水位、则需要新增以下表单内容-->
        <template v-if="currentRow.monitorType === 5 || currentRow.monitorType === 1 ">
          <el-form-item label="孔深" prop="holeDepth" >
            <el-input v-model="currentRow.holeDepth" placeholder="输入孔深" type="number" :maxlength="10" clearable>
              <i slot="suffix">m</i>
            </el-input>
          </el-form-item>

<!--          Flist = this.tableDataAuth.filter(data => data.caseCode.includes(this.searchCode))-->
<!--          <el-form-item label="孔口高程">-->
<!--            <el-input v-model="currentRow.oriHeight" placeholder="输入孔口高程" type="number" :maxlength="10" clearable>-->
<!--              <i slot="suffix">m</i>-->
<!--            </el-input>-->
<!--          </el-form-item>-->
        </template>
      </el-collapse-item>
      <el-collapse-item title="岩性分布" v-if="currentRow.monitorType === 1 && show" name="岩性分布">
        <el-button type="primary" icon="el-icon-plus"
                   @click="currentRow.lithologyList.push({colorVal: '#000000'})">
          添加
        </el-button>
        <el-form :model="currentRow" ref="imgForm" class="imgFrom" size="small">
          <el-table border :data="currentRow.lithologyList" class="m-t-24 form_table">
            <el-table-column label="开始深度(mm)" width="140">
              <template slot-scope="scope">
                <el-form-item>
                  <el-input v-model="scope.row.startDepth" placeholder="请输入" :maxlength="10" type="number"
                            style="width:100%"></el-input>
                </el-form-item>
              </template>
            </el-table-column>

            <el-table-column label="结束深度(mm)" width="140">
              <template slot-scope="scope">
                <el-form-item>
                  <el-input v-model="scope.row.endDepth" placeholder="请输入" :maxlength="10" type="number"
                            style="width:100%"></el-input>
                </el-form-item>
              </template>
            </el-table-column>

            <el-table-column label="岩性" width="140">
              <template slot-scope="scope">
                <el-form-item>
                  <el-select v-model.trim="scope.row.lithology" clearable placeholder="选择岩性">
                    <el-option v-for="item in EnumLithologyList" :key="item.code" :label="item.name"
                               :value="item.code"></el-option>
                  </el-select>
                </el-form-item>
              </template>
            </el-table-column>

            <el-table-column label="颜色" width="70">
              <template slot-scope="scope">
                <el-form-item>
                  <el-color-picker v-model="scope.row.colorVal" show-alpha></el-color-picker>

                </el-form-item>
              </template>
            </el-table-column>

            <el-table-column label="描述">
              <template slot-scope="scope">
                <el-form-item>
                  <el-input size="small" v-model="scope.row.remark" placeholder="请输入描述"
                            style="width:100%"></el-input>
                </el-form-item>
              </template>
            </el-table-column>

            <el-table-column label="操作" width="90">
              <template slot-scope="scope">
                <el-button type="text" @click="handleDelete(scope.$index, 'lithologyList')">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form>
      </el-collapse-item>
      <el-collapse-item title="相关照片" name="相关照片">
        <el-tooltip v-if="currentRow.picList.length >= 3" effect="dark" content="最多上传3张图片"
                    placement="top-start">
          <el-button class="is-disabled" size="small" type="primary">点击上传</el-button>
        </el-tooltip>
        <upload-img v-else trigger="button" avatar :size-limit="5" @update="setFormImg"></upload-img>
        <el-form :model="currentRow" ref="imgForm" class="imgFrom" size="small">
          <el-table border :data="currentRow.picList" class="m-t-24" style="height: 300px;overflow: auto">
            <el-table-column label="图片名称">
              <template slot-scope="scope">
                <el-form-item
                  :prop="`picList.${scope.$index}.name`">
                  <el-input
                    v-model="scope.row.name"
                    placeholder="请输入图片名称"
                    :maxlength="20"
                    style="width: 100%"
                  ></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="拍摄时间" width="240">
              <template slot-scope="scope">
                <el-form-item
                  :prop="`picList.${scope.$index}.time`">
                  <el-date-picker
                    v-model="scope.row.time"
                    type="datetime"
                    value-format="timestamp"
                    placeholder="选择拍摄时间">
                  </el-date-picker>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="图片" width="110">
              <template slot-scope="scope">
                <el-image
                  style="width: 60px; height: 60px"
                  fit="scale-down"
                  :src="constUrl + scope.row.url"
                ></el-image>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="90">
              <template slot-scope="scope">
                <el-button
                  type="text"
                  class="delete_button"
                  @click="handleDeleteImg(scope.$index)">删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form>
      </el-collapse-item>
    </el-collapse>
  </el-form>
</template>

<script>
import DoubleThree from '@/directives/double-three'
import {monitorPointAdd, monitorPointUpdate} from '@/services/model/project-info'
import Enums from '@/utils/enums'
import UploadImg from "@/components/upload-img";
import constUrl from "@/services/env";
import {store} from "../../work-point.store";
import Permission from '@/utils/permission'

export default {
  mixins:[Enums],
  directives: {DoubleThree},
  components: {UploadImg},
  data() {
    return {
      constUrl,
      activeName: '基础信息',
      index: 0,
      show: true,//岩性分布暂时弃用
      // form表单校验规则
      rules: {
        holeDepth: {required: true, message: '请输入孔深'},
        name: [{required: true, message: '请输入监测点名称'},{ max: 35, message: "最多只能输入35个字符", trigger: "blur" }],
        monitorType: {required: true, message: '请选择监测类型'},
      }, // 表单校验的规则
      currentRow: {
        lithologyList: [{colorVal: '#fff'}],
        warningFlag: 1,
        startEndPileInfo: {},
        picList: []
      }, // 新增数据
      // 监测类型
      EnumProjectMonitorTypeList: [],
      // 岩性分布
      EnumLithologyList: [],
      //朝向
      DirectionList: [],
    }
  },
  computed: {
    workPointSlopeDamageType() {
      const {
        workPointId
      } = this.$route.params
      const info = store.workPointList.find(x => x.id === workPointId) || {}
      return info.slopeDamageType
    },
    // 时间选择器
    timerArray: {
      get() {
        return [this.currentRow.startTime, this.currentRow.endTime]
      },
      set([startTime, endTime]) {
        this.currentRow = {...this.currentRow, startTime, endTime}
      }
    },
  },
  methods: {
    // 判断权限
    hasPermission(permissionStr) {
      return Permission(this.$store.state.userInfo, permissionStr)
    },
    changeMonitorType(){
      if(this.currentRow.monitorType!==1){
        this.currentRow.isAutoRecord=1
      }
    },
    handleDelete(index) {
      this.currentRow.lithologyList.splice(index, 1);
    },
    // 删除图片
    handleDeleteImg(index) {
      this.currentRow.picList.splice(index, 1);
    },
    // 上传成功后处理图片
    setFormImg(path) {
      let item = {
        name: "",
        shotTime: "",
        url: path,
      };
      this.currentRow.picList.push(item);
    },
    // 表单提交
    // parentResolve 从父级传入的rosolve
    validateForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          const [startTime, endTime] = this.timerArray
          this.currentRow = {...this.currentRow, startTime, endTime}
          this.submitData()
        }
      })
    },
    // 新增编辑提交数据
    async submitData() {
      // 根据ID判断使用新增还是编辑
      let action = this.currentRow.id ? monitorPointUpdate : monitorPointAdd
      let res = await action(this.currentRow)
      if (res.code === 200) {
        if (res.data) {
          this.currentRow.id = res.data
        }
        // 通知父级
        this.$emit('update', this.currentRow)
      }
    },
    // 新增数据
    handleAdd(workPointId, projectId) {
      this.activeName = '基础信息'
      this.currentRow = {
        workPointId: workPointId,
        projectId: projectId,
        warningFlag: 1,
        isPushUdp: 0,
        isAutoRecord: 1,
        lithologyList: [],
        picList: [],
        pushSn:''

      }
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
    },
    /**
     * row  当前操作行的对象
     */
    async handleEdit(row) {
      this.activeName = '基础信息'
      this.currentRow = JSON.parse(JSON.stringify(row))
      console.log(this.currentRow )

      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
    },
  },
  mounted() {
    //	监测类型
    this.queryDictList(['EnumProjectMonitorType','EnumLithology','EnumDirection'],
      ['EnumProjectMonitorTypeList', 'EnumLithologyList', 'DirectionList'])
  },
}
</script>

<style lang="less" scoped>
  /deep/ .el-collapse-item__header {
    background-color: #1b1b1b;
    margin-top: 20px;
    color: white;
    font-size: 20px;
    height: 40px;
    border-radius: 10px;

  }

  .work-point-form {
    /deep/ .collapse {
      border: 1px solid #1990FE;
      border-top: none;

      .title, .text {
      }

      .el-radio__input, .el-radio__label {
      }

      .el-radio__input.is-checked .el-radio__inner {
        background: #1990FE !important;
      }
    }

    /*.el-form-item {*/
    /*  display: inline-block;*/
    /*  width: 50%;*/
    /*}*/
  }

  .imgFrom {
    .el-form-item {
      width: 100%;

      .el-date-editor.el-input, .el-date-editor.el-input__inner {
        width: auto;
      }
    }
  }

  /deep/ .m-colorPicker {
    .box {
      position: fixed;
      bottom: 200px;

    }
  }

  .form_table {
    .el-form-item {
      // margin-bottom: 0;
    }
  }

  /deep/ .el-form-item__label {
    color: #1990FE !important;
  }

  /deep/ .collapse .title .text {
    color: white !important;
  }

  .el-button--text {
    color: red !important;
  }
</style>
